<?php
/**
 * User: krisbuist
 * Date: 2/1/12
 * Time: 12:52 PM
 */


class ChannelGenerator extends Controller
{
    protected $step = 1;

    protected $totalNumberOfChannels = 200;
    protected $totalNumberOfUsers;
    protected $totalNumberOfPosts;

    protected $minNumberOfPostsPerChannel = 1;
    protected $maxNumberOfPostsPerChannel = 15;

    public function __construct()
    {
        parent::__construct();
        $this->load->library('/generators/RandomTextGenerator');
        //$this->randomtextgenerator = new RandomtextGenerator();
        $this->totalNumberOfUsers = $this->em->createQuery("SELECT COUNT(u) FROM User u")->getSingleScalarResult();
        $this->totalNumberOfPosts = $this->em->createQuery("SELECT COUNT(p) FROM Post p")->getSingleScalarResult();
    }

    public function generateChannels()
    {
        try
        {
            for ($i = 0; $i < $this->step; $i++)
            {
                $channel = $this->generateRandomChannel();
                $this->em->persist($channel);
            }

            $this->em->flush();
        }
        catch (PDOException $e)
        {
            echo $e;
        }

        $this->forwardBrowserIfNotFinished();
    }

    public function forwardBrowserIfNotFinished()
    {
        $numberOfChannels = $this->em->createQuery("SELECT COUNT(c) FROM Channel c")->getSingleScalarResult();
        if ($numberOfChannels >= $this->totalNumberOfChannels)
        {
            echo 'Finished inserting channels!';
        }
        else
        {
            echo '<meta http-equiv="refresh" content="0;url=/generators/channelGenerator/generateChannels">';
            echo '<title>' . $numberOfChannels . '</title>';
        }
    }

    /**
     * @return Channel
     */
    private function generateRandomChannel()
    {
        $channel = new Channel();
        $channel->setName($this->randomtextgenerator->getContent(rand(2, 5), 'plain', false));
        $channel->setOwner($this->getRandomProUser());


        $posts = $this->getRandomSetOfPosts();
        foreach ($posts as $post)
        {
            $channel->addPost($post);
        }

        return $channel;
    }

    /**
     * @return User
     */
    private function getRandomProUser()
    {
        $proUser = null;
        while ($proUser == null || $proUser->getType()->getAccessLevel() != 2)
        {
            $proUser = $this->em->find('User', rand(1, $this->totalNumberOfUsers));
        }
        return $proUser;
    }

    private function getRandomSetOfPosts()
    {
        $numberOfPosts = rand($this->minNumberOfPostsPerChannel, $this->maxNumberOfPostsPerChannel);
        $posts = array();

        while (sizeof($posts) < $numberOfPosts)
        {
            $newPost = $this->getRandomPost();

            $alreadyInResult = false;
            foreach ($posts as $post)
            {
                if ($post->getId() == $newPost->getId())
                {
                    $alreadyInResult = true;
                }
            }
            if (!$alreadyInResult)
            {
                $posts[] = $newPost;
            }
        }
        return $posts;

    }


    private function getRandomPost()
    {
        $post =  null;
        while($post == null)
        {
            $post =  $this->em->find('Post', rand(1, $this->totalNumberOfPosts));
        }
        return $post;
    }
}
